package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wind.entity.Users;
import com.wind.service.IUsersService;
import com.wind.util.HttpClientUtil;
import com.wind.util.SMSUtil;
import com.wind.util.TXSMSUtil;
import com.wind.vo.ResultData;
import com.wind.vo.ResultDataGenerator;
import com.wind.wx.WXAppletUserInfo;
import com.wind.wx.WeiXinConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName UsersController
 * @Description Hey, it's compiled! Pack it!
 * @Author Administrator
 * @Date 2021/5/22 20:04
 * @Version 1.0
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UsersController {

    @Resource
    IUsersService usersService;

    @ApiOperation("小程序登录")
    @GetMapping("/wxLogin/{code}")
    public ResultData<JSONObject> login(@PathVariable("code") String code){
        JSONObject jsonObject = WXAppletUserInfo.putSmallRoutine(code);
        String openId = jsonObject.getString("openid");
        if(Objects.isNull(openId)){
            return ResultDataGenerator.genFailResult(null, "code 错误");
        }
        Wrapper<Users> wrapper = new EntityWrapper<>();
        wrapper.eq("open_id", openId);
        Users users = usersService.selectOne(wrapper);
        //第一次登录
        if(Objects.isNull(users)){
            users = new Users();
            users.setOpenId(openId);
            usersService.insert(users);
        }
        jsonObject.put("usersInfo", users);
        return ResultDataGenerator.genSuccessResult(jsonObject, "登录成功");
    }

    @ApiOperation("完善用户信息->头像、昵称")
    @PostMapping("/updateUserInfo")
    public ResultData<Users> updateUserInfo(@RequestParam("openId") String opendId,
                                     @RequestParam("nickName") String nickName,
                                     @RequestParam("headImgUrl") String headImgUrl){
        Wrapper<Users> wrapper = new EntityWrapper<>();
        wrapper.eq("open_id", opendId);
        Users users = usersService.selectOne(wrapper);
        if(Objects.isNull(users)){
            return ResultDataGenerator.genFailResult(null, "用户不存在");
        }
        users.setNickname(nickName);
        users.setPhotoPath(headImgUrl);
        usersService.updateById(users);
        return ResultDataGenerator.genSuccessResult(users, "完善信息成功");
    }

    @ApiOperation("发送短信验证码")
    @GetMapping("/sendMsg/{phone}")
    public ResultData<String> sendMsg(@PathVariable("phone") String phone){
        String result = TXSMSUtil.sendSMS(phone);
        return ResultDataGenerator.genSuccessResult(result, result);
    }

    @ApiOperation("获取用户个人信息")
    @GetMapping("/view/{id}")
    public ResultData<Users> getUserInfo(@PathVariable("id") Long id){
        Wrapper<Users> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Users user = usersService.selectOne(wrapper);
        return ResultDataGenerator.genSuccessResult(user, "获取用户信息成功");
    }


}
